<?php
/**
 * SHOP 
 * ============================================================================
 * 网络科技有限公司，并保留所有权利。
*/

define('IN_ZYS', true);
require(dirname(__FILE__) . '/includes/init.php');
//默认进来
/*支付列表*/
if ($_REQUEST['act'] == 'list'){
	 /* 查询数据库中启用的支付方式 */
    $pay_list = array();
    $sql = "SELECT * FROM ".ly()."payment  ORDER BY pay_order";
    $res = $db->query($sql);
    while ($row = $db->fetchRow($res))
    {
        $pay_list[$row['pay_code']] = $row;
    }
    /* 取得插件文件中的支付方式 */
   	$modules=$pay_list;
    assign_query_info();
    $smarty->assign('modules', $modules);
    $smarty->display('payment_list.html');
	
}
/*支付方式内容*/
elseif ($_REQUEST['act'] == 'edit')
{
    $pay_code=$_REQUEST['code'];
    $sql="select * from ".ly()."payment where pay_code='{$pay_code}'";
    $res=$db->getRow($sql);
	$arr_pay=unserialize($res['pay_config']);
	
	$arr_pay['0']['name']="支付宝账户";
	$arr_pay['1']['name']="交易安全校验码";
	$arr_pay['2']['name']="合作者身份ID";
	$arr_pay['3']['name']="选择接口类型";

	$name=serialize($arr_pay);
    $sqls = "UPDATE " .ly()."payment SET pay_config = '$name'" . "WHERE pay_code = '{$pay_code}' LIMIT 1"; ;
    $db->query($sqls);
			
	$smarty->assign('paycon', $arr_pay);
	$smarty->assign('pay', $res);
    $smarty->display('payment_edit.html');
}
/*提交支付方式*/
elseif ($_REQUEST['act']=='Submit')
{
    admin_priv('payment');

    /* 检查输入 */
    if (empty($_POST['pay_name']))
    {
        sys_msg($_LANG['payment_name'] . $_LANG['empty']);
    }
	/* 取得配置信息 */
    $pay_config = array();
    if (isset($_POST['cfg_value']) && is_array($_POST['cfg_value']))
    {
        for ($i = 0; $i < count($_POST['cfg_value']); $i++)
        {
            $pay_config[] = array('name'  => trim($_POST['cfg_name'][$i]),
                                  'type'  => trim($_POST['cfg_type'][$i]),
                                  'value' => trim($_POST['cfg_value'][$i])
            );
        }
    }

    $fig = serialize($pay_config);
	
    /* 取得和验证支付手续费 */
    $pay_fee= empty($_POST['pay_fee'])?0:$_POST['pay_fee'];

    /* 检查是编辑还是安装 */
    $link[] = array('text' => $_LANG['back_list'], 'href' => 'payment.php?act=list');
    if ($_POST['pay_id'])
    {
        /* 编辑 */
         $sql = "UPDATE " .ly()."payment 
                  SET pay_name = '$_POST[pay_name]'," .
                   "    pay_desc = '$_POST[pay_desc]'," .
                   "    pay_config = '$fig'," .
                   "    pay_fee    =  '$pay_fee'".
                   "WHERE pay_code = '$_POST[pay_code]' LIMIT 1";
            $db->query($sql);
        /* 记录日志 */
        admin_log($_POST['pay_name'], 'edit', 'payment');

       $href[]=array('text'=>"返回支付方式",'href'=>"payment.php?act=list");
        sys_msg("编辑成功",$href);	
    }
    else
    {
        /* 安装，检查该支付方式是否曾经安装过 */
        $sql = "SELECT COUNT(*) FROM " .ly()."payment  WHERE pay_code = '$_REQUEST[pay_code]'";

        if ($db->GetOne($sql) > 0)
        {
            /* 该支付方式已经安装过, 将该支付方式的状态设置为 enable */
            $sql = "UPDATE " .ly()."payment 
                  SET pay_name = '$_POST[pay_name]'," .
                   "    pay_desc = '$_POST[pay_desc]'," .
                   "    pay_config = '$fig'," .
                   "    pay_fee    =  '$pay_fee', ".
                   "    enabled = '1' " .
                   "WHERE pay_code = '$_POST[pay_code]' LIMIT 1";
            $db->query($sql);
        }
        else
        {
            /* 该支付方式没有安装过, 将该支付方式的信息添加到数据库 */
            $sql = "INSERT INTO " .ly()."payment (pay_code, pay_name, pay_desc, pay_config, is_cod, pay_fee, enabled, is_online)" .
                   "VALUES ('$_POST[pay_code]', '$_POST[pay_name]', '$_POST[pay_desc]', '$fig', '$_POST[is_cod]', '$pay_fee', 1, '$_POST[is_online]')";
            $db->query($sql);
        }

        /* 记录日志 */
        admin_log($_POST['pay_name'], 'install', 'payment');
        $href[]=array('text'=>"返回支付方式",'href'=>"payment.php?act=list");
    sys_msg("编辑成功",$href);
    }
}
?>
